package com.authine.cloudpivot.web.api.secondopen.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.secondopen.modules.SameRoadNotifyOrderResult;
import com.authine.cloudpivot.web.api.secondopen.service.SameRoadBusinessTravelService;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: brookpeng
 * @Date: 2021/12/14 15:44
 */
@Api(value = "二开接口", tags = "同程商旅对接")
@RestController
@RequestMapping("/api/sameRoadBusiness")
public class SameRoadBusinessTravelController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(SameRoadBusinessTravelController.class);

    @Autowired
    private SameRoadBusinessTravelService sameRoadBusinessTravelService;

    /**
     * 同程商旅员工信息同步
     *
     * @return
     */
    @RequestMapping(value = "/syncEmployeeInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "同程商旅员工信息同步", notes = "同程商旅员工信息同步")
    public ResponseResult<String> syncEmployeeInfo() {

        String syncResult = sameRoadBusinessTravelService.syncEmployeeInfo();
        return getOkResponseResult(syncResult, "success");
    }

    /**
     * 同程商旅外部差旅单同步
     *
     * @return
     */
    @RequestMapping(value = "/syncTravelApplyOrder", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "同程商旅外部差旅单同步", notes = "同程商旅外部差旅单同步")
    public ResponseResult<String> syncTravelApplyOrder(@RequestParam(value = "sequenceNo", required = true) String sequenceNo) {
        String ccsqdTableName = getBizObjectFacade().getTableName("ccsqd");
        String ccsqdTableSubTableName = getBizObjectFacade().getTableName("timeAndPlace");
        String sameRoadApplyOrderSyncTableName = getBizObjectFacade().getTableName("SameRoadApplyOrderSync");
        String syncResult = sameRoadBusinessTravelService.syncTravelApplyOrder(ccsqdTableName, sequenceNo, ccsqdTableSubTableName, sameRoadApplyOrderSyncTableName);
        return getOkResponseResult(syncResult, "success");
    }

    /**
     * 同程商旅对账单
     * @param pageIndex
     * @param pageSize
     * @param beginDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/sameRoadAccountCheck", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "同程商旅对账单", notes = "同程商旅对账单")
    public ResponseResult<String> sameRoadAccountCheck(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,
                                                       @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                                       @RequestParam(value = "beginDate", required = true) String beginDate,
                                                       @RequestParam(value = "endDate", required = true) String endDate) {
        String tableName = getBizObjectFacade().getTableName("NoteStatementPageLis");
        String result = sameRoadBusinessTravelService.sameRoadAccountCheck(pageIndex, pageSize, beginDate, endDate, tableName);
        return getOkResponseResult(result, "success");
    }

    /**
     * 查询机票对账单
     * @param statementCode
     * @param StatementStatus
     * @return
     */
    @RequestMapping(value = "/QueryOnlineStatementFlightDetailPageList", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "查询机票对账单", notes = "查询机票对账单")
    public ResponseResult<String> queryOnlineStatementFlightDetailPageList(@RequestParam(value = "statementCode", required = true) String statementCode,
                                                                           @RequestParam(value = "StatementStatus", required = true) String StatementStatus) {
        String result = sameRoadBusinessTravelService.queryOnlineStatementFlightDetailPageList(statementCode, StatementStatus);
        return getOkResponseResult(result, "success");
    }

    /**
     * 查询火车对账单
     * @param statementCode
     * @param statusList
     * @return
     */
    @RequestMapping(value = "/QueryOnlineStatementTrainDetailPageList", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "查询火车对账单", notes = "查询火车对账单")
    public ResponseResult<String> queryOnlineStatementTrainDetailPageList(@RequestParam(value = "statementCode", required = true) String statementCode,
                                                                          @RequestParam(value = "statusList", required = true) String statusList) {
        sameRoadBusinessTravelService.queryOnlineStatementTrainDetailPageList(statementCode, statusList);
        return null;
    }

    @RequestMapping(value = "/QueryOnlineStatementHotelDetailPageList", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "查询酒店对账单", notes = "查询酒店对账单")
    public ResponseResult<String> queryOnlineStatementHotelDetailPageList(@RequestParam(value = "statementCode", required = true) String statementCode,
                                                                          @RequestParam(value = "statusList", required = true) String statusList){

        return null;
    }

    /**
     * 订单数据回推
     * @param orderInfo
     */
    @PostMapping(value = "/takeNotify")
    public SameRoadNotifyOrderResult takeNotify(@RequestParam(name = "orderInfo") String orderInfo) {
        log.info("※※※同程商旅订单数据回推:{}※※※", orderInfo);
        SameRoadNotifyOrderResult sameRoadNotifyOrderResult = new SameRoadNotifyOrderResult();
        JSONObject jsonObject = JSON.parseObject(orderInfo);
        int notifyType = jsonObject.getInteger("notifyType");
        // 机票订单推送
        if (1 == notifyType) {
            String tableName = getBizObjectFacade().getTableName("NoteFlightOrderPushBack");
            sameRoadBusinessTravelService.noteFlightOrder(jsonObject, tableName);
        }
        sameRoadNotifyOrderResult.setResCode("100");
        sameRoadNotifyOrderResult.setResMsg("success");
        return sameRoadNotifyOrderResult;
    }

    @RequestMapping(value = "/getFlightOrderList", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "查询同程机票订单列表接口", notes = "查询同程机票订单列表接口")
    public ResponseResult<String> getFlightOrderList() {
        String tableName = getBizObjectFacade().getTableName("NoteFlightOrderPushBack");
        sameRoadBusinessTravelService.saveFlightOrderList();
        return null;
    }
}
